package LinkTest;
//合并两个有序链表为一个有序链表
public class SumLink {
   static class ListNode {
    int val;
    ListNode next = null;
    public ListNode(int val) {
      this.val = val;
    }
 }
    public static ListNode Merge (ListNode pHead1, ListNode pHead2) {
        if(pHead1==null&&pHead2==null){
            return pHead1;
        }
        // write code here
        ListNode head=new ListNode(0);
        ListNode k=head;
        ListNode h1=pHead1;
        ListNode h2=pHead2;
        while(h1!=null&&h2!=null){
            if(h1.val<=h2.val){
                k.next=h1;
                k=h1;
                h1=h1.next;
            }else{
                k.next=h2;
                k=h2;
                h2=h2.next;
            }
        }
        if(h1!=null){
            k.next=h1;
            h1=h1.next;
        }else{
            k.next=h2;
            h2=h2.next;
        }
        return head.next;
    }
}
